//
//  WCNPiccPatientListVC.m
//  WCN
//
//  Created by wsj2015 on 16/3/30.
//  Copyright © 2016年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNPiccPatientListVC.h"
#import "WCButton.h"
#import "UINavigationItem+Offset.h"
#import "WCNNewPICCPreserveVC.h"
#import "WCNPatientHomeTCell.h"
#import "WCNPatientHospitalTCell.h"
#import "WCNPiccPatientCell.h"
#import "WCNPiccRecordListVC.h"
#import "UITableViewRowAction+JZExtension.h"
#import "JCAlertView.h"
@interface WCNPiccPatientListVC ()


@property (nonatomic,  strong) NSMutableArray *hosPatientArr;

@property (nonatomic,  strong) NSMutableArray *clinicPatientArr;

@property (nonatomic,  strong) NSMutableArray *otherPatientArr;

@property (nonatomic, assign) NSInteger totalPage;
@end

@implementation WCNPiccPatientListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupUI];
    [self.tableView.header beginRefreshing];
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.hosPatientArr =[NSMutableArray array];
        self.clinicPatientArr = [NSMutableArray array];
        self.otherPatientArr = [NSMutableArray array];
        
    }
    return self;
}
- (void)setupUI{
    self.view.backgroundColor = kAppBg;
    self.title = @"PICC患者";
    WCButton *rightBtn = [WCButton wcButtonWithType:WCButtonTypeAdd];
    [rightBtn addTarget:self action:@selector(add) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem addRightBarButtonItem:rightBtn];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"WCNPiccPatientCell" bundle:nil] forCellReuseIdentifier:@"WCNPiccPatientCell"];
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64);
    defineWeakSelf;
    [self.tableView addLegendHeaderWithRefreshingBlock:^{
        [weakSelf postPiccPatientList:YES];
    }];
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        [weakSelf postPiccPatientList:NO];
    }];
    [self.tableView gzwLoading:^{
        [weakSelf postPiccPatientList:YES];
    }];
    
}
- (void)add{
    
    weakSelf_define;
   WCNNewPICCPreserveVC *vc = [WCNNewPICCPreserveVC new];
    vc.isAutoRefresh = ^(){
        [weakSelf.tableView.header beginRefreshing];
    };
    [self.navigationController pushViewController:vc animated:YES];
   
}
- (void)postPiccPatientList:(BOOL)isRecover{
    
    
    static NSInteger page =1;
    if (isRecover) {
        page =1;
       
    }else{
    
        page++;
    }
    CoreSVPLoading(@"加载中...", NO);
    defineWeakSelf;
    self.tableView.loading = YES;
    NSMutableDictionary * params =[NSMutableDictionary dictionary];
    params[@"page"] =@(page);
    params[@"rows"] =@(10);
    
    params[@"nurseId"] = @([[WCDefaultManager shareMg] nurseId]);
//    params[@"patientType"] 默认不传
    [[WCNetworkAccessManager sharedMg] POST:kGetPiccPatientList parameters:params success:^(NSDictionary *responseObject) {
        if (isRecover) {
            [weakSelf.hosPatientArr removeAllObjects];
            [weakSelf.clinicPatientArr removeAllObjects];
            [weakSelf.otherPatientArr removeAllObjects];
        }
        weakSelf.totalPage = [responseObject[@"data"][@"totalPage"] integerValue];
        NSArray *arrray = responseObject[@"data"][@"result"];
        if ([arrray isKindOfClass:[NSArray class]]) {
            for (NSDictionary *dict in arrray) {
                WCNMyPatientModel *model =[WCNMyPatientModel objectWithKeyValues:dict];
                if ([model.patientType integerValue]==1) {
                    [weakSelf.hosPatientArr addObject:model];
                }else if([model.patientType integerValue]==2){
                    [weakSelf.clinicPatientArr addObject:model];
                }else if([model.patientType integerValue]==3){
                    [weakSelf.otherPatientArr addObject:model];
                }
            }
        }
        if ((page-1)==[responseObject[@"totalPage"] integerValue]) {
            [weakSelf.tableView.footer endRefreshing];
            [weakSelf.tableView.footer noticeNoMoreData];
            
        }
        [weakSelf.tableView reloadData];
    } failure:nil netError:nil always:^{
        [CoreSVP dismiss];
       
        if (page > 1) {
            [weakSelf.tableView.footer endRefreshing];
            if (page - 1 == weakSelf.totalPage) {
                
                [weakSelf.tableView.footer noticeNoMoreData];
            }
        }else {
            [weakSelf.tableView.header endRefreshing];
            [weakSelf.tableView.footer resetNoMoreData];
        }
//        [weakSelf.tableView.footer endRefreshing];
        weakSelf.tableView.loading =NO;
    }];
    
}

- (void)delePatientRecordPost:(NSInteger)recoreId{

    CoreSVPLoading(@"删除中...", NO);
    weakSelf_define;
    [[WCNetworkAccessManager sharedMg] POST:kDelePatientPiccRecord parameters:@{@"id":@(recoreId)} success:^(NSDictionary *responseObject) {
        CoreSVPSuccess(responseObject[@"data"]);
        [weakSelf.tableView.header beginRefreshing];
    } failure:nil netError:nil always:^{
        [CoreSVP  dismiss];
        [weakSelf setEditing:false animated:true];
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger num =0;
    if (self.hosPatientArr.count>0) {
        num++;
    }
    if (self.clinicPatientArr.count>0) {
        num++;
    }
    if (self.otherPatientArr.count>0) {
        num++;
    }
    
    return num;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section ==0) {
        return  self.hosPatientArr.count;
    }else if(section ==1){
        return self.clinicPatientArr.count;
    }
    return self.otherPatientArr.count;
}
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"住院患者";
    }else if(section ==1){
        return @"门诊患者";
    }else{
        return @"其他";
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WCNPiccPatientCell *cell =[tableView dequeueReusableCellWithIdentifier:@"WCNPiccPatientCell" forIndexPath:indexPath];
    WCNMyPatientModel *model;
    switch (indexPath.section) {
        case 0:
            
            model = self.hosPatientArr[indexPath.row];
            break;
        case 1:
            
            model = self.clinicPatientArr[indexPath.row];
            break;
        case 3:
            
            model = self.otherPatientArr[indexPath.row];
            break;
            
        default:
            break;
    }
    
    cell.model = model;
    return cell;;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    WCNPiccRecordListVC *vc =[WCNPiccRecordListVC new];
    WCNMyPatientModel *model;
    switch (indexPath.section) {
        case 0:
            
            model = self.hosPatientArr[indexPath.row];
            break;
        case 1:
            
            model = self.clinicPatientArr[indexPath.row];
            break;
        case 3:
            
            model = self.otherPatientArr[indexPath.row];
            break;
            
        default:
            break;
    }
    
    vc.patientName = model.name;
    vc.piccId = [model.piccId integerValue];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self setEditing:false animated:true];
}

#pragma mark -适配8.0 以前版本
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    defineWeakSelf;
    
    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除"  handler:^(UITableViewRowAction * _Nullable action, NSIndexPath * _Nullable indexPath) {
        
        [JCAlertView showTwoButtonsWithTitle:@"提示" Message:@"是否确认删除" ButtonType:0 ButtonTitle:@"否" Click:nil ButtonType:2 ButtonTitle:@"是的" Click:^{
            [weakSelf setEditing:false animated:YES];
            WCNMyPatientModel *model;
            switch (indexPath.section) {
                case 0:
                    
                    model = self.hosPatientArr[indexPath.row];
                    break;
                case 1:
                    
                    model = self.clinicPatientArr[indexPath.row];
                    break;
                case 3:
                    
                    model = self.otherPatientArr[indexPath.row];
                    break;
                    
                default:
                    break;
            }
            
            
            [weakSelf delePatientRecordPost:[model.piccId integerValue]];
        }];
       
        
    }];
  
    return @[action1];
}

@end
